//============================================================================
// Name        : player.cpp
// Author      : dfoelber
//============================================================================

#include "object/player.h"
#include "image_manager.h"
#include "draw/SDL_draw.h"
#include "util.h"

/**
* Constructor
* 
* @param image the image the player
* @param enemy If the player is an enemy (mirror flip)
* @param color The color of the player
**/
PlayerObject::PlayerObject( Surface* image, bool enemy, Color color ) : Object( image, OBJECT_ID_PLAYER, true )
{
	staticImage = Util::ReplaceColor(image, COLOR_BLUE, color);
	if( enemy )
	{
		Surface* temp = staticImage;
		staticImage = Util::Flip(temp, FLIP_HORIZONTAL);
		delete temp;
	}
	
	animIndex = animCounter = 0;
	isCasting = false;
	colorKey = SDL_MapRGB(image->GetSurface()->format, 255, 255, 255);
	
	SetBackground( COLOR_BLACK, 0 );
	
	this->image = ImageManager::GetManager()->CreateImage(staticImage->GetWidth(),staticImage->GetHeight());
	SDL_SetColorKey(this->image->GetSurface(), SDL_SRCCOLORKEY | SDL_RLEACCEL, colorKey);
	Draw_FillRect( this->image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
	staticImage->Apply(this->image, 0, 0);
	
	CreateAnimations(enemy, color);
}

/**
* Deconstructor
**/
PlayerObject::~PlayerObject()
{
	if (image)
	{
		delete image;
		image = NULL;
	}
	
	if (staticImage)
	{
		delete staticImage;
		staticImage = NULL;
	}
	
	for( int i = 0; i < (int)anims.size(); i++ )
	{
		delete anims[i];
	}
}

/**
* Lets the object know that time has passed and to move accordingly.
* 
* @param ms The time passed since last called
**/
void PlayerObject::TimePassed( int ms )
{
	Object::TimePassed(ms);
	
	if( isCasting )
	{
		animCounter += ms;
		if( animCounter >= PLAYER_CASTING_ANIM_DELAY )
		{
			if( (int)anims.size() > 0 )
			{
				if( animIndex >= (int)anims.size() )
				{
					isCasting = false;
					Draw_FillRect( image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
					staticImage->Apply(image, 0, 0);
					return;
				}
				
				Draw_FillRect( image->GetSurface(), 0, 0, image->GetWidth(), image->GetHeight(), COLOR_WHITE.ToUint32() );
				anims[animIndex]->Apply(image, 0, 0);
				
				animIndex++;
				
			}
			
			animCounter = 0;
		}
	}
}

/**
* Starts a casting animation.
**/
void PlayerObject::StartCastAnimation()
{
	animCounter = animIndex = 0;
	isCasting = true;
}

/**
* Creates the animation vector
**/
void PlayerObject::CreateAnimations(bool enemy, Color color)
{
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_1), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_2), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_3), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_4), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_5), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_6), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_7), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_8), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_9), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_10), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_11), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_12), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_13), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_14), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_15), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_16), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_17), enemy, color );
	PushBack( ImageManager::GetManager()->GetImage(IMAGE_GOOD_WIZARD_ANIM_18), enemy, color );
}

/**
* Creates and pushes back a specific image
**/
void PlayerObject::PushBack(Surface* stock, bool enemy, Color color)
{
	Surface* image = Util::ReplaceColor(stock, COLOR_BLUE, color);
	if( enemy )
	{
		Surface* temp = image;
		image = Util::Flip(temp, FLIP_HORIZONTAL);
		delete temp;
	}
	anims.push_back(image);
}
